package com.droidexchange.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ListAdapter;
import com.commonsware.cwac.endless.EndlessAdapter;
import com.droidexchange.R;
import com.droidexchange.util.Util;

public class PagingAdapter extends EndlessAdapter
{
	private RotateAnimation	rotate		= null;
	private IPagingAdapter	callback	= null;

	public PagingAdapter(IPagingAdapter callback, ListAdapter listAdapter)
	{
		super(listAdapter);
		this.callback = callback;
		Init();
	}

//	@Override
//	public View getView(int position, View convertView, ViewGroup parent)
//	{
//		return (getWrappedAdapter().getView(position, convertView, parent));
//	}

	private void Init()
	{
		rotate = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
		rotate.setDuration(600);
		rotate.setRepeatMode(Animation.RESTART);
		rotate.setRepeatCount(Animation.INFINITE);
	}

	@Override
	protected View getPendingView(ViewGroup parent)
	{
		Util.log("getPendingView()");
		View row = LayoutInflater.from(callback.getContext()).inflate(R.layout.endless_row, null);
		View child = row.findViewById(android.R.id.text1);
		child.setVisibility(View.GONE);

		child = row.findViewById(R.id.throbber);
		child.setVisibility(View.VISIBLE);
		child.startAnimation(rotate);

		return (row);
	}

	@Override
	protected boolean cacheInBackground()
	{
		return callback.cacheInBackground();
	}

	@Override
	protected void appendCachedData()
	{
		callback.appendCachedData(getWrappedAdapter());
	}
}
